package com.example.pingapplication2.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.pingapplication2.R;
import com.example.pingapplication2.databinding.FragmentInstrumentBinding;
import com.example.pingapplication2.net.INetListener;
import com.example.pingapplication2.viewModel.StateViewModel;

/**
 * 仪器界面
 */
public class InstrumentFragment extends Fragment implements INetListener {
    FragmentInstrumentBinding fragmentInstrumentBinding;
    StateViewModel mStateViewModel;

    public InstrumentFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_instrument, container, false);
        fragmentInstrumentBinding = FragmentInstrumentBinding.bind(view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //viewModel的初始化
        mStateViewModel = new ViewModelProvider(requireActivity()).get(StateViewModel.class);

        fragmentInstrumentBinding.dashboard.setMaxNum(100);
        fragmentInstrumentBinding.dashboard.setStartNum(0);
        fragmentInstrumentBinding.dashboard.setUnit("%");
        fragmentInstrumentBinding.dashboard.setTextSize(50);

        mStateViewModel.packetLoss.observe(getViewLifecycleOwner(), new Observer<Double>() {
            @Override
            public void onChanged(Double aDouble) {
                fragmentInstrumentBinding.dashboard.setPercent(aDouble.intValue());
            }
        });
    }



    @Override
    public void pingSuccess() {

    }

    @Override
    public void pingFailure() {

    }
}